package com.mobartisan.workinglunch.service;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent; 

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

/**
 * Created by zhaorui on 16-8-7.
 */
public class InternalExchangeService {
    private static Hashtable<String, Object> exchangeCache;
    public static String KEY_EXCHANGE_PARAM = "_KEY_EXCHANGE_PARAM";
    public static String KEY_EXCHANGE_REQUEST_COODE = "KEY_EXCHANGE_REQUEST_COODE";
    private static int requestCodeSeed = 4;

    public static void registerExchangeParam(Intent intent, Object param) {
        String key = new SimpleDateFormat().format(new Date());
        intent.putExtra(KEY_EXCHANGE_PARAM, key);
        if (exchangeCache == null) exchangeCache = new Hashtable<String, Object>();
        synchronized (exchangeCache) {
            exchangeCache.put(key, param);
        }
    }

    public static Object getExchangeParam(Activity activity) {
        return getExchangeParam(activity.getIntent());
    }

    public static Object getFragment(Fragment fragment) {
        Activity activity = fragment.getActivity();
        if (activity == null) return null;
        return getExchangeParam(activity.getIntent());
    }

    public static Object getExchangeParam(Intent intent) {
        if (intent == null) return null;
        if (exchangeCache == null) return null;
        String key = intent.getStringExtra(KEY_EXCHANGE_PARAM);
        if(null==key||"".equals(key)) return null;
        synchronized (exchangeCache) {
            Object o = exchangeCache.get(key);
            exchangeCache.remove(key);
            return o;
        }
    }

    public static int startActivityForResult(Activity activity, Intent newIntent) {
        int requestCode = requestCodeSeed++;
        newIntent.putExtra(KEY_EXCHANGE_REQUEST_COODE, requestCode);
        activity.startActivityForResult(newIntent, requestCode);
        return requestCode;
    }

    public static void startActivity(Activity activity, Intent intent) {
        activity.startActivity(intent);
        //activity.overridePendingTransition(android.R.anim.slide_out_right,android.R.anim.slide_in_left);
    }

    public static void finishActivityForResult(Activity activity, Intent toIntent) {
        int requestCode = activity.getIntent().getIntExtra(KEY_EXCHANGE_REQUEST_COODE, -1);
        int resultCode = 0;
        finishActivityForResult(activity, requestCode, resultCode, toIntent);
    }

    public static void finishActivityForResult(Activity activity, int resultCode, Intent toIntent)
    {
        int requestCode = activity.getIntent().getIntExtra(KEY_EXCHANGE_REQUEST_COODE, -1);
        finishActivityForResult(activity, requestCode, resultCode, toIntent);
    }

    private static void finishActivityForResult(Activity activity, int requestCode, int resultCode, Intent toIntent) {
        if (toIntent == null)
            activity.setResult(resultCode);
        else activity.setResult(resultCode, toIntent);
        if (requestCode < 0)
            activity.finish();
        else {
            activity.finishActivity(requestCode);
            activity.finish();
        }
    }
}
